package me.khrystal.rxnews.module.news.ui;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import me.khrystal.rxnews.R;
import me.khrystal.rxnews.annotation.ActivityFragmentInject;
import me.khrystal.rxnews.app.AppManager;
import me.khrystal.rxnews.base.BaseActivity;
import me.khrystal.rxnews.base.BaseFragment;
import me.khrystal.rxnews.base.BaseFragmentAdapter;
import me.khrystal.rxnews.greendao.NewsChannelTable;
import me.khrystal.rxnews.module.news.presenter.INewsPresenter;
import me.khrystal.rxnews.module.news.presenter.INewsPresenterImpl;
import me.khrystal.rxnews.module.news.view.INewsView;
import me.khrystal.rxnews.utils.RxBus;
import me.khrystal.rxnews.utils.ViewUtil;
import rx.Observable;
import rx.functions.Action1;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/18
 * update time:
 * email: 723526676@qq.com
 */
@ActivityFragmentInject(contentViewId = R.layout.activity_news,
        menuId =  R.menu.menu_news,
        hasNavigationView = true,
        toolbarTitle =  R.drawable.ic_list_white,
        toolbarIndicator = R.drawable.ic_list_white,
        menuDefaultCheckedItem = R.id.action_news)
public class NewsActivity extends BaseActivity<INewsPresenter> implements INewsView{

    private Observable<Boolean> mChannelObservable;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister("channelChange",mChannelObservable);
    }

    @Override
    protected void initView() {
        //设置默认windowBackground使得启动没那么突兀 在设置为空 减少过度绘制
        getWindow().setBackgroundDrawable(null);
        ViewUtil.quitFullScreen(this);

        AppManager.getAppManager().orderNavActivity(getClass().getName(),false);
        mPresenter = new INewsPresenterImpl(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_channel_manage){
            //TODO 跳转频道选择界面
            showActivity(this,new Intent(this,NewsChannelActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initViewPager(List<NewsChannelTable> newsChannels) {
        TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);

        final ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);

        List<BaseFragment> fragments = new ArrayList<>();
        final List<String> title = new ArrayList<>();

        if (newsChannels != null){
            for (NewsChannelTable news : newsChannels){
                final NewsListFragment fragment = NewsListFragment.newInstance(
                        news.getNewsChannelId(),news.getNewsChannelType(),
                        news.getNewsChannelIndex());

                fragments.add(fragment);
                title.add(news.getNewsChannelName());
            }

            if (viewPager.getAdapter() == null){
                BaseFragmentAdapter adapter = new BaseFragmentAdapter(getSupportFragmentManager(),
                        fragments,title);
                viewPager.setAdapter(adapter);
            }else {
                final BaseFragmentAdapter adapter = (BaseFragmentAdapter)viewPager.getAdapter();
                adapter.updateFragments(fragments,title);
            }
            viewPager.setCurrentItem(0,false);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setScrollPosition(0,0,true);
//            根据Tab长度动态设置TabLayout的模式
            ViewUtil.dynamicSetTablayoutMode(tabLayout);
        }else {
            toast("数据异常");
        }

    }

    @Override
    public void initRxBusEvent() {
        mChannelObservable = RxBus.get().register("channelChange",Boolean.class);
        mChannelObservable.subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (aBoolean)
                    mPresenter.onprateChannelDb();
            }
        });
    }
}
